package com.lonewalker.completablefuturedemo;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author LoneWalker
 * @date 2023/2/12
 * @description
 */
public class demo3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
//            System.out.println(Thread.currentThread().getName());
//            return 5;
//        }).whenComplete((result,exception)->{
//            System.out.println(Thread.currentThread().getName());
//            if (result != null){
//                result += 5;
//            }
//
//            if (exception != null){
//                System.out.println(exception.getMessage());
//            }
//        });
//        System.out.println("主线程表示没闲着");
//        System.out.println("执行结果："+completableFuture1.get());


//        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
//            System.out.println(Thread.currentThread().getName());
//            try {
//                Thread.sleep(2000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            return 6;
//        }).whenCompleteAsync((result,exception)->{
//            System.out.println(Thread.currentThread().getName());
//            if (result != null){
//                result += 5;
//            }
//        });
//
//        System.out.println("执行结果："+completableFuture2.get());

        CompletableFuture<Integer> completableFuture3 = CompletableFuture.supplyAsync(() -> {
            int a = 2/0;
            return 7;
        }).exceptionally(exception ->{
            System.out.println("有异常返回默认值");
            return 5;
        });
        System.out.println(completableFuture3.join());
    }
}
